package com.zyb.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.ordervo.CourseWebVoOrder;
import com.zyb.commonutils.ordervo.UcenterMemberOrder;
import com.zyb.eduorder.client.EduClient;
import com.zyb.eduorder.client.UcenterClient;
import com.zyb.eduorder.entity.Order;
import com.zyb.eduorder.entity.vo.OrderQuery;
import com.zyb.eduorder.mapper.OrderMapper;
import com.zyb.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyb.eduorder.utils.OrderNoUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testJava
 * @since 2022-04-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private EduClient eduClient;
    @Resource
    private UcenterClient ucenterClient;

    //1 生成订单的方法
    @Override
    public String createOrders(String courseId, String memberId) {
        //通过远程调用根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        //通过远程调用根据课程id获取课信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());
        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }

    //条件查询带分页订单列表
    @Override
    public IPage<Order> getOrderPage(Long page, Long limit, OrderQuery orderQuery) {
        IPage<Order> iPage=new Page<>(page,limit);
        QueryWrapper<Order> wrapper=new QueryWrapper<>();

        String orderNo = orderQuery.getOrderNo();
        String courseTitle = orderQuery.getCourseTitle();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        if (!StringUtils.isEmpty(orderNo)){
            wrapper.like("order_no",orderNo);
        }
        if (!StringUtils.isEmpty(courseTitle)){
            wrapper.like("course_title",courseTitle);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(iPage,wrapper);
        return iPage;
    }
}
